package cc.yiueil.Interrupt;

/**
 * 多重线程中断
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        t.start();
        Thread.sleep(1000);
        // 中断t线程
        t.interrupt();
        // 等待t线程结束
        t.join();
        System.out.println("end");
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        Thread hello = new HelloThread();
        // 启动hello线程
        hello.start();
        try {
            // 等待hello线程结束
            hello.join();
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
        // 如果不写下面一句, 会发现hello线程仍然执行中, jvm不会停止
        // hello.interrupt();
    }
}

class HelloThread extends Thread {
    @Override
    public void run() {
        int n = 0;
        while (!isInterrupted()) {
            n++;
            System.out.println(n + " hello!");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("helloThread interrupted!");
                break;
            }
        }
    }
}